//
//  MinorViewController.m
//  StockNetManager
//
//  Created by 小焘无量 on 2023/3/22.
//

#import "MinorViewController.h"
#import "MemberDetailViewController.h"
#import "MinorTableViewCell.h"

@interface MinorViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *memberBtn;
@property (weak, nonatomic) IBOutlet UIButton *reportBtn;
@property (weak, nonatomic) IBOutlet UIButton *priceBtn;
@property (weak, nonatomic) IBOutlet UIButton *stockBtn;

@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, assign) CheckType checkType;
@property (nonatomic, strong) UIButton *currentBtn;

@end

@implementation MinorViewController

#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIConfig];
    
    [self getListNetworkIsMore:NO showLoading:NO];
}

-(void)dealloc {
    //[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - view layout
- (void)UIConfig {
    
    self.navigationItem.title = @"次控";
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:MinorTableViewCellID bundle:nil] forCellReuseIdentifier:MinorTableViewCellID];
    
    
    [_tableView addHeaderWithTarget:self action:@selector(loadNewData)];
    [_tableView addFooterWithTarget:self action:@selector(loadMoreData)];
    
    self.checkType = CheckType_Member;
    self.currentBtn = self.memberBtn;
}

-(void)loadNewData {
    [self getListNetworkIsMore:NO showLoading:NO];
}

-(void)loadMoreData {
    [self getListNetworkIsMore:YES showLoading:NO];
}



#pragma mark - Event Response
- (IBAction)memberBtnAction:(UIButton *)sender {
    if (sender == self.currentBtn) {
        return;
    }
    self.checkType = CheckType_Member;
    [self changeBtnBgColor:sender];
    [self getListNetworkIsMore:NO showLoading:YES];
}

- (IBAction)reportBtnAction:(UIButton *)sender {
    if (sender == self.currentBtn) {
        return;
    }
    self.checkType = CheckType_Report;
    [self changeBtnBgColor:sender];
    [self getListNetworkIsMore:NO showLoading:YES];
}
- (IBAction)priceBtnAction:(UIButton *)sender {
    if (sender == self.currentBtn) {
        return;
    }
    self.checkType = CheckType_Price;
    [self changeBtnBgColor:sender];
    [self getListNetworkIsMore:NO showLoading:YES];
}
- (IBAction)stockBtnAction:(UIButton *)sender {
    if (sender == self.currentBtn) {
        return;
    }
    self.checkType = CheckType_Stock;
    [self changeBtnBgColor:sender];
    [self getListNetworkIsMore:NO showLoading:YES];
    
}

#pragma mark - Delegate,DataSource
#pragma mark  UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MinorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MinorTableViewCellID forIndexPath:indexPath];
    cell.model = self.modelArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UserInfoModel *model = self.modelArr[indexPath.row];
    MemberDetailViewController *vc = [[MemberDetailViewController alloc]initWithModel:model];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Network

-(void)getListNetworkIsMore:(BOOL)isMore showLoading:(BOOL)show {
    
    self.pageNum = isMore ? self.pageNum : 1;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@(self.checkType) forKey:@"type"];
    [dic setObject:@"" forKey:@"current"];
    [dic setObject:NETPageSize forKey:@"size"];
    
    WeakSelf;
    
    if (show) {
        [MBProgressHUD showLoadingMessage:nil view:self.view];
    }
    
    [[APIManager manager] Get:URL_checkList parameters:dic success:^(id  _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view];
        if (NET_RESULT_CODE == 200) {
            
            NSArray *arr = responseObject[@"data"][@"records"];
            NSMutableArray *modelArr = [[NSMutableArray alloc]init];
            for (NSDictionary *dic in arr) {
                MinorListModel *model = [MinorListModel mj_objectWithKeyValues:dic];
                model.checkType = weakSelf.checkType;
                [modelArr addObject:model];
            }
            if (isMore) {
                [weakSelf.modelArr addObjectsFromArray:modelArr];
            }else {
                weakSelf.modelArr = [NSMutableArray arrayWithArray:modelArr];
            }
            
            weakSelf.pageNum++;
            [weakSelf.tableView reloadData];
            [weakSelf.tableView endRefreshing];
            
            NSInteger totalCount = [responseObject[@"data"][@"total"] integerValue];
            if (weakSelf.modelArr.count < totalCount) {
                [weakSelf.tableView updateFooterWithEnd:NO];
            }else {
                [weakSelf.tableView updateFooterWithEnd:YES];
            }
            
        }else {
            NET_SHOW_MSG;
            [weakSelf.tableView endRefreshing];
        }
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:weakSelf.view];
        NET_SHOW_ERROR;
        [weakSelf.tableView endRefreshing];
    }];
    
}

#pragma mark - Method
-(void)changeBtnBgColor:(UIButton *)btn{
    
    [self.memberBtn setBackgroundColor:kRGB(0x9A9A9A)];
    [self.reportBtn setBackgroundColor:kRGB(0x9A9A9A)];
    [self.priceBtn setBackgroundColor:kRGB(0x9A9A9A)];
    [self.stockBtn setBackgroundColor:kRGB(0x9A9A9A)];
    
    [btn setBackgroundColor:kThemeColor];
    
    self.currentBtn = btn;
    
}

#pragma mark - Set Get

#pragma mark - Lazy
-(NSMutableArray *)modelArr{
    if (!_modelArr) {
        _modelArr = [[NSMutableArray alloc]init];
    }
    return _modelArr;
}

@end
